home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / Fresco / build / Unix / config / util / mdepend.cpp < prev    next >
Encoding:
Text File  |  1995-07-12  |  4.0 KB  |  240 lines

  1. XCOMM!/bin/sh
  2. XCOMM
  3. XCOMM $XConsortium: mdepend.cpp,v 1.12 94/12/09 22:50:34 gildea Exp $
  4. XCOMM
  5. XCOMM    Do the equivalent of the 'makedepend' program, but do it right.
  6. XCOMM
  7. XCOMM    Usage:
  8. XCOMM
  9. XCOMM    makedepend [cpp-flags] [-w width] [-s magic-string] [-f makefile]
  10. XCOMM      [-o object-suffix]
  11. XCOMM
  12. XCOMM    Notes:
  13. XCOMM
  14. XCOMM    The C compiler used can be overridden with the environment
  15. XCOMM    variable "CC".
  16. XCOMM
  17. XCOMM    The "-v" switch of the "makedepend" program is not supported.
  18. XCOMM
  19. XCOMM
  20. XCOMM    This script should
  21. XCOMM    work on both USG and BSD systems.  However, when System V.4 comes out,
  22. XCOMM    USG users will probably have to change "silent" to "-s" instead of
  23. XCOMM    "-" (at least, that is what the documentation implies).
  24. XCOMM
  25. CC=PREPROC
  26.  
  27. silent='-'
  28.  
  29. TMP=/tmp/mdep$$
  30. CPPCMD=${TMP}a
  31. DEPENDLINES=${TMP}b
  32. TMPMAKEFILE=${TMP}c
  33. MAGICLINE=${TMP}d
  34. ARGS=${TMP}e
  35.  
  36. trap "rm -f ${TMP}*; exit 1" 1 2 15
  37. trap "rm -f ${TMP}*; exit 0" 1 2 13
  38.  
  39. echo " \c" > $CPPCMD
  40. if [ `wc -c < $CPPCMD` -eq 1 ]
  41. then
  42.     c="\c"
  43.     n=
  44. else
  45.     c=
  46.     n="-n"
  47. fi
  48.  
  49. echo $n "$c" >$ARGS
  50.  
  51. files=
  52. makefile=
  53. magic_string='# DO NOT DELETE'
  54. objsuffix='.o'
  55. width=78
  56. endmarker=""
  57. verbose=n
  58. append=n
  59.  
  60. while [ $# != 0 ]
  61. do
  62.     if [ "$endmarker"x != x ] && [ "$endmarker" = "$1" ]; then
  63.     endmarker=""
  64.     else
  65.     case "$1" in
  66.         -D*|-I*)
  67.         echo $n " '$1'$c" >> $ARGS
  68.         ;;
  69.  
  70.         -g|-O)    # ignore so we can just pass $(CFLAGS) in
  71.         ;;
  72.  
  73.         *)
  74.         if [ "$endmarker"x = x ]; then
  75.             case "$1" in     
  76.             -w)
  77.                 width="$2"
  78.                 shift
  79.                 ;;
  80.             -s)
  81.                 magic_string="$2"
  82.                 shift
  83.                 ;;
  84.             -f)
  85.                 makefile="$2"
  86.                 shift
  87.                 ;;
  88.             -o)
  89.                 objsuffix="$2"
  90.                 shift
  91.                 ;;
  92.             
  93.             --*)
  94.                 echo "$1" | sed 's/^\-\-//' >${TMP}end
  95.                 endmarker="`cat ${TMP}end`"
  96.                 rm -f ${TMP}end
  97.                 if [ "$endmarker"x = x ]; then
  98.                 endmarker="--"
  99.                 fi
  100.                 ;;
  101.             -v)
  102.                 verbose="y"
  103.                 ;;
  104.  
  105.             -a)
  106.                 append="y"
  107.                 ;;
  108.  
  109.             -cc)
  110.                 CC="$2"
  111.                 shift
  112.                 ;;
  113.  
  114.             -*)
  115.                 echo "Unknown option '$1' ignored" 1>&2
  116.                 ;;
  117.             *)
  118.                 files="$files $1"
  119.                 ;;
  120.             esac
  121.         fi
  122.         ;;
  123.     esac
  124.     fi
  125.     shift
  126. done
  127. echo ' $*' >> $ARGS
  128.  
  129. echo "#!/bin/sh" > $CPPCMD
  130. echo "exec $CC `cat $ARGS`" >> $CPPCMD
  131. chmod +x $CPPCMD
  132. rm $ARGS
  133.  
  134. case "$makefile" in
  135.     '')
  136.     if [ -r makefile ]
  137.     then
  138.         makefile=makefile
  139.     elif [ -r Makefile ]
  140.     then
  141.         makefile=Makefile
  142.     else
  143.         echo 'no makefile or Makefile found' 1>&2
  144.         exit 1
  145.     fi
  146.     ;;
  147.     -)
  148.     makefile=$TMPMAKEFILE
  149.     ;;
  150. esac
  151.  
  152. if [ "$verbose"x = "y"x ]; then 
  153.     cat $CPPCMD
  154. fi
  155.  
  156. echo '' > $DEPENDLINES
  157. for i in $files
  158. do
  159.     $CPPCMD $i \
  160.       | sed -n "/^#/s;^;$i ;p"
  161. done \
  162.   | sed -e 's|/[^/.][^/]*/\.\.||g' -e 's|/\.[^.][^/]*/\.\.||g' \
  163.     -e 's|"||g' -e 's| \./| |' \
  164.   | awk '{
  165.     if ($1 != $4  &&  $2 != "#ident" && $2 != "#pragma")
  166.         {
  167.         ofile = substr ($1, 1, length ($1) - 2) "'"$objsuffix"'"
  168.         print ofile, $4
  169.         }
  170.     }' \
  171.   | sort -u \
  172.   | awk '
  173.         {
  174.         newrec = rec " " $2
  175.         if ($1 != old1)
  176.         {
  177.         old1 = $1
  178.         if (rec != "")
  179.             print rec
  180.         rec = $1 ": " $2
  181.         }
  182.         else if (length (newrec) > '"$width"')
  183.         {
  184.         print rec
  185.         rec = $1 ": " $2
  186.         }
  187.         else
  188.         rec = newrec
  189.         }
  190.     END \
  191.         {
  192.         if (rec != "")
  193.         print rec
  194.         }' \
  195.   | egrep -v '^[^:]*:[     ]*$' >> $DEPENDLINES
  196.  
  197. trap "" 1 2 13 15    # Now we are committed
  198. case "$makefile" in
  199.     $TMPMAKEFILE)
  200.     ;;
  201.     *)
  202.     rm -f $makefile.bak
  203.     cp $makefile $makefile.bak
  204.     echo "Appending dependencies to $makefile"
  205.     ;;
  206. esac
  207.  
  208. XCOMM
  209. XCOMM If not -a, append the magic string and a blank line so that
  210. XCOMM /^$magic_string/+1,\$d can be used to delete everything from after
  211. XCOMM the magic string to the end of the file.  Then, append a blank
  212. XCOMM line again and then the dependencies.
  213. XCOMM
  214. if [ "$append" = "n" ]
  215. then
  216.     cat >> $makefile << END_OF_APPEND
  217.  
  218. $magic_string
  219.  
  220. END_OF_APPEND
  221.     ed $silent $makefile << END_OF_ED_SCRIPT
  222. /^$magic_string/+1,\$d
  223. w
  224. q
  225. END_OF_ED_SCRIPT
  226.     echo '' >>$makefile
  227. fi
  228.  
  229. cat $DEPENDLINES >>$makefile
  230.  
  231. case "$makefile" in
  232.     $TMPMAKEFILE)
  233.     cat $TMPMAKEFILE
  234.     ;;
  235.  
  236. esac
  237.  
  238. rm -f ${TMP}*
  239. exit 0
  240.